import { ref } from "vue";
export function useBookmark() {
  const bookmarkDict = ref([]);
  chrome.bookmarks.getTree(function (tree) {
    // 获取书签根目录下的所有书签
    const bookmarks = tree[0].children[0].children;
    // 遍历所有书签
    for (let i = 0; i < bookmarks.length; i++) {
      const bookmark = bookmarks[i];
      const title = bookmark.title;
      const url = bookmark.url;
      const faviconUrl = getFaviconUrl(url);
      bookmarkDict.value.push({
        bookmark,
        title,
        url,
        faviconUrl,
      });
    }
    // 将生成的HTML代码插入到页面中
    console.log(bookmarkDict.value);
  });

  // 目前最全 https://api.xinac.net/icon/?url=
  // favicon大全 -> https://ico.cxr.cool/
  // "https://www.google.cn/s2/favicons?domain=" + encodeURIComponent(url);
  //https://favicon.yandex.net/favicon +url
  // url + "/favicon.ico";
  const getFaviconUrl = (url) => {
    return "https://api.xinac.net/icon/?url=" + url;
  };

  return {
    bookmarkDict,
  };
}
